package com.powerbosframework.web.form;
import java.lang.reflect.Field;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;

import com.powerbosframework.log.LogFactory;
import com.powerbosframework.log.Logger;
import com.powerbosframework.web.action.SupportAction;
import com.powerbosframework.web.form.FormBeanHelp;
/**
 * 控制器表单类
 * @author youfu.wang
 * @version 1.0
 */
public class ActionForm {
	private HttpServletRequest request;	
	protected boolean isExsitEntityBean=false;
	private Map formProperties = new HashMap();	
	private Logger log=LogFactory.getInstance();
	
	public ActionForm(){
		
	}
	public ActionForm(HttpServletRequest request){
		this.request=request;	
	}
	//处理表单映射
	public void processActionForm(SupportAction supportAction) throws SecurityException,InstantiationException, IllegalAccessException{
		FormBeanHelp formBeanHelp=new FormBeanHelp(this.request,supportAction);
		for (Enumeration enu = request.getParameterNames(); enu.hasMoreElements();) {
			String name = (String) enu.nextElement();
			String values[] = request.getParameterValues(name);
			if (values != null)
				if (values.length == 1) {
					if(formBeanHelp.isEntityBean(name)){
						isExsitEntityBean=true;
						formBeanHelp.createFormBean(new FormProperty( name,values[0]));
					}else{
						formBeanHelp.setProperty(supportAction, new FormProperty( name,values[0]));
					}					
					formProperties.put(name, values[0]);
				} else {
					if(formBeanHelp.isEntityBean(name)){
						isExsitEntityBean=true;
						formBeanHelp.createFormBean(new FormProperty( name,values));
					}else{
						formBeanHelp.setProperty(supportAction, new FormProperty( name,values));
					}					
					formProperties.put(name, values);
				}
		}
		if(isExsitEntityBean){
			formBeanHelp.setFormBean();
		}
	
	}	
	protected void setParameterValue(String name,String value){
		if(formProperties.containsKey(name)){						
			formProperties.remove(name);
		}
		formProperties.put(name, value);
	}
	public String getParameterValue(String name){
		String value = "";
		if (name == null || name.equals(""))
			return "";
		Object obj = formProperties.get(name);
		if (obj == null)
			return value;
		if (obj instanceof String)
			value = (String) obj;
		if (obj instanceof String[]) {
			String arr[] = (String[]) obj;
			if (arr.length > 0)
				value = arr[0];
		}
		if (value == null)
			return "";			
		return value;
		
	}
	public String[] getParameterValues(String name) {
		String values[] = (String[]) null;
		Object obj = formProperties.get(name);
		if (obj == null)
			return null;
		if (obj instanceof String) {
			values = new String[1];		
			values[0] = (String) obj;			
		}
		if (obj instanceof String[]) {
			values = (String[]) obj;
			if (values.length > 0) {
				for (int i = 0; i < values.length; i++)
					values[i] = values[i];					
			}
		}
		return values;
	}
}
